﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using System.IO;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;

namespace hzGis.service.impl
{
    public class FileService : IFileService
    {
        StyleService styleService = new StyleService();
        public void openShpFile(string shpFilePath, AxMapControl axMapControl)
        {
            Dictionary<string, object> result = openShpFilePro(shpFilePath, axMapControl);
            ILayer pLayer = result["layer"] as ILayer;
            styleService.setDLZWBStyle(pLayer);
            styleService.setLabelStyle(pLayer, "DLBM");
            axMapControl.Map.AddLayer(pLayer);
        }



        public Dictionary<string, object> openShpFilePro(string shpFilePath, AxMapControl axMapControl)
        {
            IFeatureClass pFeatureClass = this.openFeatureClassByShp(shpFilePath);
            IDataset pDataset = pFeatureClass as IDataset;
            //声明要素图层，设置相应属性
            IFeatureLayer pFeatureLayer = new FeatureLayer();
            pFeatureLayer.FeatureClass = pFeatureClass;
            pFeatureLayer.Name = pDataset.Name;
            //将要素图层转为图层，Add to MapControl 
            ILayer pLayer = pFeatureLayer as ILayer;
            Dictionary<string, object> result = new Dictionary<string, object>();
            IGeoDataset featureDataset = pFeatureClass as IGeoDataset;
            IEnvelope featureExtent = featureDataset.Extent;
            double x = (featureExtent.XMin + featureExtent.XMax) / 2;
            double y = (featureExtent.YMin + featureExtent.YMax) / 2;
            IPoint center = new ESRI.ArcGIS.Geometry.Point();
            center.X = x;
            center.Y = y;
            result.Add("layer", pLayer);
            result.Add("center", center);
            return result;
        }


        public IFeatureClass openFeatureClassByShp(string shpFilePath)
        {
            string pathName = System.IO.Path.GetDirectoryName(shpFilePath);
            string fileName = System.IO.Path.GetFileNameWithoutExtension(shpFilePath);
            //声明Shape文件工作空间工厂，并获取要素工作空间，加载要素类
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pathName, 0);
            IFeatureWorkspace pFeatureWorkspace;
            pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);
            return pFeatureClass;
        }


        public ILayer openLayerFile(string layerFilePath)
        {
            if (File.Exists(layerFilePath))
            {
                ILayerFile layerFile = new LayerFile();
                layerFile.Open(layerFilePath);
                ILayer layer = layerFile.Layer;
                return layer;
            }
            else
            {
                return null;
            }
        }

        public string GetPath(IFeatureClass ifc)
        {
            string fullpath = string.Empty;
            FeatureDataset ifdt = ifc as FeatureDataset;
            if (ifdt != null)
            {
                string path = ifdt.Workspace.PathName;
                if (ifc.FeatureDataset != null)
                {
                    path += "\\" + ifc.FeatureDataset.Name;
                }
                fullpath = path;
            }
            return fullpath;
        }

        public string getCurrentPath()
        {
            string str = this.GetType().Assembly.Location;
            string dirPath = System.IO.Path.GetDirectoryName(str);
            return dirPath;
        }

    }
}
